package com.kq.tallybookl.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kq.tallybookl.R;
import com.kq.tallybookl.database.FakeDBHelper;
import com.kq.tallybookl.database.FakeManager;
import com.kq.tallybookl.database.TrueManager;
import com.kq.tallybookl.item.MenoItem;
import com.kq.tallybookl.util.GetTime;

/**
 * 编辑界面
 * Created by Kq on 2016/5/1.
 */
public class EditActivity extends Activity{

    private boolean verificationStatus;

    private FakeManager fakeManager;
    private TrueManager trueManager;

    private EditText editTextTitle;
    private EditText editTextDetails;
    private Button buttonBack;
    private TextView textViewDay;
    private TextView textViewHour;

    private String title;
    private String details;
    private long time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        initView();
        getIntentInformation();
        initViewInformation();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        textViewDay = (TextView) findViewById(R.id.edit_textView_time_above);
        textViewHour = (TextView) findViewById(R.id.edit_textView_time_bottom);
        editTextTitle = (EditText) findViewById(R.id.edit_editText_title);
        editTextDetails = (EditText) findViewById(R.id.edit_editText_details);
        buttonBack = (Button) findViewById(R.id.edit_button_back);
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化控件信息
     */
    private void initViewInformation() {
        textViewDay.setText(GetTime.getYearTimeString(time));
        textViewHour.setText(GetTime.getBottomTimeString(time));
        editTextTitle.setText(title);
        editTextDetails.setText(details);
    }

    /**
     * 获取intent的信息,并初始化信息
     */
    private void getIntentInformation() {
        Intent intent = this.getIntent();
        verificationStatus = false;
        title = "";
        details = "";
        time = System.currentTimeMillis();
        if(intent!=null){
            verificationStatus = intent.getBooleanExtra(PreferencesActivity.VERIFICATION_STATUS,false);
            title = intent.getStringExtra(PreferencesActivity.MENOITEM_TITLE);
            details = intent.getStringExtra(PreferencesActivity.MENOITEM_DETAILS);
            time = intent.getLongExtra(PreferencesActivity.MENOITEM_TIME,System.currentTimeMillis());
        }
        if(title == null){
            title = "";
        }
        if(details == null){
            details = "";
        }
        fakeManager = new FakeManager(this);
        String key = fakeManager.getLoginPassword();
        trueManager = new TrueManager(this,key);
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveItem();
    }

    private void saveItem(){
        String etTitle = editTextTitle.getText().toString();
        String etDetails = editTextDetails.getText().toString();
        Intent intent = new Intent(EditActivity.this,TrueActivity.class);
        intent.putExtra(PreferencesActivity.VERIFICATION_STATUS,verificationStatus);
        if(etTitle.equals(title)&&etDetails.equals(details)){
            startActivity(intent);
            finish();
        }else {
            if(verificationStatus){
                if(title.equals("")&&details.equals("")) {
                    trueManager.addMenoItem(new MenoItem(etTitle, System.currentTimeMillis(), etDetails));
                }else {
                    trueManager.updateMenoItem(
                            new MenoItem(title, time, details),
                            new MenoItem(etTitle, System.currentTimeMillis(), etDetails));
                }
            }else{
                if(title.equals("")&&details.equals("")) {
                    fakeManager.addMenoItem(new MenoItem(etTitle, System.currentTimeMillis(), etDetails));
                }else {
                    fakeManager.updateMenoItem(
                            new MenoItem(title, time, details),
                            new MenoItem(etTitle, System.currentTimeMillis(), etDetails));
                }
            }
            startActivity(intent);
            finish();
        }
    }
}
